//
//  CollectViewController.m
//  UGoods
//
//  Created by 李贺 on 16/3/1.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseManager.h"
#import "LHAFNetworkTool.h"
#import "FindModel.h"
#import "BaseTableView.h"
#import "BaseView.h"
#import "BaseLabel.h"
#import "BuyDetailViewController.h"
#import "ShoppingTableViewCell.h"
#import <MJRefresh.h>
#import "ConstantsDefine.h"

@interface CollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation CollectViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}

// 创建收藏列表
- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[ShoppingTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FindModel *model = self.dataArray[indexPath.row];
    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
    FindModel *model = self.dataArray[indexPath.row];
    buy.model = model;
    [self.navigationController pushViewController:buy animated:YES];
    [buy release];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [[DataBaseManager shareInstance] openDB];
    self.dataArray = [NSMutableArray array];
    self.dataArray = [[DataBaseManager shareInstance] select];
    [self.tableView reloadData];
    if (_dataArray.count != 0) {
        self.title = @"土豪 做朋友吧";
    } else {
        self.title = @"穷逼 啥也没有";
        BaseView *backView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 350 * HEIGHT)];
        //    backView.backgroundColor = [UIColor redColor];
        self.tableView.tableHeaderView = backView;
        [backView release];
        
        UIImageView *aimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 100 * HEIGHT)];
        aimageView.center = backView.center;
        aimageView.image = [UIImage imageNamed:@"iconfont-gouwu-5"];
        [backView addSubview:aimageView];
        [aimageView release];
        
        BaseLabel *alabel = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, 300 * WIDTH, 20 * HEIGHT)];
        alabel.center = CGPointMake(backView.center.x, backView.frame.size.height - 80 * HEIGHT);
        alabel.text = @"唉呀妈呀，你的购物袋空空如也 ! !";
        alabel.font = [UIFont fontWithName:@"Zapfino" size:15];
        alabel.textAlignment = NSTextAlignmentCenter;
        [backView addSubview:alabel];
        [alabel release];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
